home *** CD-ROM | disk | FTP | other *** search
/ PC Open 107 / PC Open 107 CD 1.bin / CD1 / INTERNET / COPIA SITI / Getleft / getleft-setup-notcl.exe / {app} / scripts / tablelistMove.tcl < prev    next >
Encoding:
Text File  |  2003-12-28  |  8.9 KB  |  338 lines

  1. #==============================================================================
  2. # Contains the implementation of the tablelist move and movecolumn subcommands.
  3. #
  4. # Copyright (c) 2003-2004  Csaba Nemethi (E-mail: csaba.nemethi@t-online.de)
  5. #==============================================================================
  6.  
  7. #------------------------------------------------------------------------------
  8. # tablelist::moveSubCmd
  9. #
  10. # This procedure is invoked to process the tablelist move subcommand.
  11. #------------------------------------------------------------------------------
  12. proc tablelist::moveSubCmd {win source target} {
  13.     upvar ::tablelist::ns${win}::data data
  14.  
  15.     if {$data(isDisabled) || $data(itemCount) == 0} {
  16.     return ""
  17.     }
  18.  
  19.     #
  20.     # Adjust the indices to fit within the existing items and check them
  21.     #
  22.     if {$source > $data(lastRow)} {
  23.     set source $data(lastRow)
  24.     } elseif {$source < 0} {
  25.     set source 0
  26.     }
  27.     if {$target > $data(itemCount)} {
  28.     set target $data(itemCount)
  29.     } elseif {$target < 0} {
  30.     set target 0
  31.     }
  32.     if {$target == $source} {
  33.     return -code error \
  34.            "cannot move item with index \"$source\" before itself"
  35.     } elseif {$target == $source + 1} {
  36.     return ""
  37.     }
  38.  
  39.     #
  40.     # Save some data of the edit window if present
  41.     #
  42.     if {[set editCol $data(editCol)] >= 0} {
  43.     set editRow $data(editRow)
  44.     set editKey $data(editKey)
  45.     saveEditData $win
  46.     }
  47.  
  48.     #
  49.     # Check whether the source line is selected and then delete it
  50.     #
  51.     set w $data(body)
  52.     set textIdx1 [expr {double($source + 1)}]
  53.     set textIdx2 [expr {double($source + 2)}]
  54.     if {[lsearch -exact [$w tag names $textIdx1] select] >= 0} {
  55.     set selected 1
  56.     } else {
  57.     set selected 0
  58.     }
  59.     $w delete $textIdx1 $textIdx2
  60.  
  61.     #
  62.     # Insert the source item before the target one
  63.     #
  64.     set target1 $target
  65.     if {$source < $target} {
  66.     incr target1 -1
  67.     }
  68.     set targetLine [expr {$target1 + 1}]
  69.     $w insert $targetLine.0 \n
  70.     set snipStr $data(-snipstring)
  71.     set sourceItem [lindex $data(itemList) $source]
  72.     set dispItem [strToDispStr $sourceItem]
  73.     set key [lindex $sourceItem end]
  74.     array set tagData [array get data $key*-\[bf\]*]        ;# for speed
  75.     set rowTags [array names tagData $key-\[bf\]*]
  76.     set col 0
  77.     foreach text [lrange $dispItem 0 $data(lastCol)] \
  78.         colFont $data(colFontList) \
  79.         colTags $data(colTagsList) \
  80.         fmtCmdFlag $data(fmtCmdFlagList) \
  81.         {pixels alignment} $data(colList) {
  82.     if {$data($col-hide)} {
  83.         incr col
  84.         continue
  85.     }
  86.  
  87.     #
  88.     # Adjust the cell text and the image width
  89.     #
  90.     if {$fmtCmdFlag} {
  91.         set text [uplevel #0 $data($col-formatcommand) \
  92.               [list [lindex $sourceItem $col]]]
  93.         set text [strToDispStr $text]
  94.     }
  95.     if {[info exists data($key-$col-image)]} {
  96.         set image $data($key-$col-image)
  97.         set imageWidth [image width $image]
  98.     } else {
  99.         set image ""
  100.         set imageWidth 0
  101.     }
  102.     if {[info exists data($key-$col-font)]} {
  103.         set cellFont $data($key-$col-font)
  104.     } elseif {[info exists data($key-font)]} {
  105.         set cellFont $data($key-font)
  106.     } else {
  107.         set cellFont $colFont
  108.     }
  109.     if {$pixels == 0} {            ;# convention: dynamic width
  110.         if {$data($col-maxPixels) > 0 &&
  111.         $data($col-reqPixels) > $data($col-maxPixels)} {
  112.         set pixels $data($col-maxPixels)
  113.         }
  114.     }
  115.     if {$pixels != 0} {
  116.         incr pixels $data($col-delta)
  117.     }
  118.     adjustElem $win text imageWidth $cellFont \
  119.            $pixels $alignment $snipStr
  120.  
  121.     #
  122.     # Insert the text and the image
  123.     #
  124.     set cellTags [array names tagData $key-$col-\[bf\]*]
  125.     set tagNames [concat $colTags $rowTags $cellTags]
  126.     if {$imageWidth == 0} {
  127.         $w insert $targetLine.end \t$text\t $tagNames
  128.     } else {
  129.         $w insert $targetLine.end \t\t $tagNames
  130.         insertElem $w $targetLine.end-1c $text $image $imageWidth $alignment
  131.     }
  132.  
  133.     incr col
  134.     }
  135.  
  136.     #
  137.     # Update the item list
  138.     #
  139.     set data(itemList) [lreplace $data(itemList) $source $source]
  140.     if {$target == $data(itemCount)} {
  141.     lappend data(itemList) $sourceItem    ;# this works much faster
  142.     } else {
  143.     set data(itemList) [linsert $data(itemList) $target1 $sourceItem]
  144.     }
  145.  
  146.     #
  147.     # Update the list variable if present
  148.     #
  149.     if {$data(hasListVar)} {
  150.     trace vdelete ::$data(-listvariable) wu $data(listVarTraceCmd)
  151.     upvar #0 $data(-listvariable) var
  152.     set var [lreplace $var $source $source]
  153.     set pureSourceItem [lrange $sourceItem 0 $data(lastCol)]
  154.     if {$target == $data(itemCount)} {
  155.         lappend var $pureSourceItem        ;# this works much faster
  156.     } else {
  157.         set var [linsert $var $target1 $pureSourceItem]
  158.     }
  159.     trace variable ::$data(-listvariable) wu $data(listVarTraceCmd)
  160.     }
  161.  
  162.     #
  163.     # Update anchorIdx and activeIdx if needed
  164.     #
  165.     if {$data(anchorIdx) == $source} {
  166.     set data(anchorIdx) $target1
  167.     }
  168.     if {$data(activeIdx) == $source} {
  169.     set data(activeIdx) $target1
  170.     }
  171.  
  172.     #
  173.     # Restore the stripes in the body text widget
  174.     #
  175.     makeStripesWhenIdle $win
  176.  
  177.     #
  178.     # Select the source item if it was selected before
  179.     #
  180.     if {$selected} {
  181.     selectionSubCmd $win set $target1 $target1
  182.     }
  183.  
  184.     #
  185.     # Restore the edit window if it was present before
  186.     #
  187.     if {$editCol >= 0} {
  188.     if {$editRow == $source} {
  189.         editcellSubCmd $win $target1 $editCol 1
  190.     } else {
  191.         set data(editRow) [lsearch $data(itemList) "* $editKey"]
  192.     }
  193.     }
  194.  
  195.     return ""
  196. }
  197.  
  198. #------------------------------------------------------------------------------
  199. # tablelist::movecolumnSubCmd
  200. #
  201. # This procedure is invoked to process the tablelist movecolumn subcommand.
  202. #------------------------------------------------------------------------------
  203. proc tablelist::movecolumnSubCmd {win source target} {
  204.     upvar ::tablelist::ns${win}::data data
  205.  
  206.     if {$data(isDisabled)} {
  207.     return ""
  208.     }
  209.  
  210.     #
  211.     # Check the indices
  212.     #
  213.     if {$target == $source} {
  214.     return -code error \
  215.            "cannot move column with index \"$source\" before itself"
  216.     } elseif {$target == $source + 1} {
  217.     return ""
  218.     }
  219.  
  220.     #
  221.     # Update the column list
  222.     #
  223.     set source3 [expr {3*$source}]
  224.     set source3Plus2 [expr {$source3 + 2}]
  225.     set target1 $target
  226.     set target3 [expr {3*$target}]
  227.     if {$source < $target} {
  228.     incr target1 -1
  229.     incr target3 -3
  230.     }
  231.     set sourceRange [lrange $data(-columns) $source3 $source3Plus2]
  232.     set data(-columns) [lreplace $data(-columns) $source3 $source3Plus2]
  233.     set data(-columns) [eval linsert {$data(-columns)} $target3 $sourceRange]
  234.  
  235.     #
  236.     # Save some elements of data corresponding to source
  237.     #
  238.     array set tmp [array get data $source-*]
  239.     array set tmp [array get data k*-$source-*]
  240.     set tmp(editCol) $data(editCol)
  241.     set tmp(arrowCol) $data(arrowCol)
  242.     set tmp(sortCol) $data(sortCol)
  243.  
  244.     #
  245.     # Remove source from the list of stretchable columns
  246.     # if it was explicitly specified as stretchable
  247.     #
  248.     if {[string first $data(-stretch) all] != 0} {
  249.     set sourceIsStretchable 0
  250.     set stretchableCols {}
  251.     foreach elem $data(-stretch) {
  252.         if {[string first $elem end] != 0 && $elem == $source} {
  253.         set sourceIsStretchable 1
  254.         } else {
  255.         lappend stretchableCols $elem
  256.         }
  257.     }
  258.     set data(-stretch) $stretchableCols
  259.     }
  260.  
  261.     #
  262.     # Build two lists of column numbers, neeeded
  263.     # for shifting some elements of the data array
  264.     #
  265.     if {$source < $target} {
  266.     for {set n $source} {$n < $target1} {incr n} {
  267.         lappend oldCols [expr {$n + 1}]
  268.         lappend newCols $n
  269.     }
  270.     } else {
  271.     for {set n $source} {$n > $target} {incr n -1} {
  272.         lappend oldCols [expr {$n - 1}]
  273.         lappend newCols $n
  274.     }
  275.     }
  276.  
  277.     #
  278.     # Move the elements of data corresponding to the columns in oldCols to the
  279.     # elements corresponding to the columns with the same indices in newCols
  280.     #
  281.     foreach oldCol $oldCols newCol $newCols {
  282.     moveColData $win data data imgs $oldCol $newCol
  283.     }
  284.  
  285.     #
  286.     # Move the elements of data corresponding to
  287.     # source to the elements corresponding to target1
  288.     #
  289.     moveColData $win tmp data imgs $source $target1
  290.  
  291.     #
  292.     # If the column given by source was explicitly specified as
  293.     # stretchable then add target1 to the list of stretchable columns
  294.     #
  295.     if {[string first $data(-stretch) all] != 0 && $sourceIsStretchable} {
  296.     lappend data(-stretch) $target1
  297.     sortStretchableColList $win
  298.     }
  299.  
  300.     #
  301.     # Update the item list
  302.     #
  303.     set newItemList {}
  304.     foreach item $data(itemList) {
  305.     set sourceText [lindex $item $source]
  306.     set item [lreplace $item $source $source]
  307.     set item [linsert $item $target1 $sourceText]
  308.     lappend newItemList $item
  309.     }
  310.     set data(itemList) $newItemList
  311.  
  312.     #
  313.     # Update the list variable if present
  314.     #
  315.     condUpdateListVar $win
  316.  
  317.     #
  318.     # Set up and adjust the columns, and rebuild
  319.     # the lists of the column fonts and tag names
  320.     #
  321.     setupColumns $win $data(-columns) 0
  322.     makeColFontAndTagLists $win
  323.     adjustColumns $win {} 0
  324.  
  325.     #
  326.     # Reconfigure the relevant column labels
  327.     #
  328.     foreach col [lappend newCols $target1] {
  329.     reconfigColLabels $win imgs $col
  330.     }
  331.  
  332.     #
  333.     # Make sure the items will be redisplayed at idle time
  334.     #
  335.     redisplayWhenIdle $win
  336.     return ""
  337. }
  338.